package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.cloudbroker.bcs.platform.dav.middle.entity.*;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.cloudbroker.bcs.common.entity.HSBCPSBackNumVO;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.util.DateTimeUtil;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ClientErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.QuestionErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.AnswerStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.DeleteStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.PaySource;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.PublicStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.UpvoteStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.type.QuestionRequestType;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.QuestionAnswerBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.result.AnswerData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.result.QuestionListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.result.TenantQuestionListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuesAnsListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuesAnswerForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuesAnswerUpdateForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuesAskForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuesAskNumForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.QuestionIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.question.vo.TenantQuesAskForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.TenantKeyListQryForm;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entityview.AnswerView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.QuestionView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.TenantAnswerView;
import com.cloudbroker.bcs.platform.dav.middle.extservice.point.PointAddService;
import com.cloudbroker.bcs.platform.dav.middle.service.AnswerService;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerBrokerFollowService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerBrokerRelService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerService;
import com.cloudbroker.bcs.platform.dav.middle.service.PayWaterService;
import com.cloudbroker.bcs.platform.dav.middle.service.QuestionService;
import com.cloudbroker.bcs.platform.dav.middle.service.TenantService;
import com.cloudbroker.bcs.platform.dav.middle.service.UpvoteWaterService;
import com.cloudbroker.bcs.platform.dav.middle.util.PositionStrUtil;
import com.cloudbroker.bcs.platform.dav.middle.util.SearchUtil;
import com.cloudbroker.bcs.platform.dav.middle.util.point.PointType;

@Service
public class QuestionAnswerBizServiceImpl implements QuestionAnswerBizService {
    
    @Resource
    private CustomerService customerService;
    
    @Resource
    private QuestionService questionService;
    
    @Resource
    private AnswerService answerService;
    
    @Resource
    private CustomerBrokerRelService customerBrokerRelService;
    
    @Resource
    private UpvoteWaterService upvoteWaterService;
    
    @Resource
    private PayWaterService payWaterService;
    
    @Resource
    private BrokerService brokerService;
    
    @Resource
    private CustomerBrokerFollowService customerBrokerFollowService;
    
    @Resource
    private TenantService tenantService;
    
    @Resource
    private PointAddService pointAddService;
    
    public static final Integer QUESTION_TYPE = 0;
    
    @Override
    public QuestionListResult funcQuestionAnswerListByFans(QuesAnsListForm param) {
        QuestionListResult result = new QuestionListResult();
        
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        Customer customer = HSBCPSBizDataContext.getCustomerNoException();
        if (String.valueOf(QuestionRequestType.CUSTOMER.getVal()).equals(String.valueOf(param.getRequest_type()))
                && customer == null) {
            return VOUtil.setSuccessResult(result);
        }
        
        customer = customer == null ? new Customer() : customer;
        String question_tag = param.getQuestion_tag();
        String position_str = param.getPosition_str();
        
        Integer request_num = SysContants.DEFAULT_REQUSET_NUM;
        if (!StringUtil.isEmpty(param.getRequest_num())) {
            request_num = Integer.parseInt(param.getRequest_num());
        }
        Question question = new Question();
        question.setBroker_id(broker_id);
        question.setPosition_str(position_str);
        question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
        if (!StringUtil.isEmpty(question_tag)) {
            question.setQuestion_tag(question_tag);
        }
        // 查看自己的提问
        Long customer_id = customer.getCustomer_id();
        if (!StringUtil.isEmpty(param.getRequest_type())
                && String.valueOf(QuestionRequestType.CUSTOMER.getVal()).equals(
                        String.valueOf(param.getRequest_type()))) {
            question.setCustomer_id(customer_id);
        } else {
//            // 粉丝只能看到公开的问题
//            question.setPublic_flag(String.valueOf(PublicStatus.PUBLIC.getVal()));
            // 粉丝只能看到没删除的问题
            question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
            // 粉丝只能看到已回答的问题
            question.setAnswer_status(String.valueOf(AnswerStatus.ANSWERD.getVal()));
            question.setCustomer_id(customer_id); //sql做了处理，查出问答中粉丝提问的确还没被回答的问题
        }
        Pagination page = new Pagination();
        page.setPage_size(request_num);
        question.setPage(page);
        
        List<QuestionView> dataList = new ArrayList<QuestionView>();
        
        List<Question> questions = this.questionService.query(question);
        if (questions != null && !questions.isEmpty()) {
            List<Long> questionIds = new ArrayList<>();
            List<Long> customerIds = new ArrayList<>();
            for (Question q : questions) {
                questionIds.add(q.getQuestion_id());
                if (!customerIds.contains(q.getCustomer_id())) {
                    customerIds.add(q.getCustomer_id());
                }
            }
            //查出点赞
            List<Long> ups = this.upvoteWaterService.queryByCustomerId(customer.getCustomer_id(), broker_id,
                    questionIds, String.valueOf(UpvoteStatus.QUESTION_UPVOTE.getVal()));
            List<Long> payViewIds = this.payWaterService.queryPayViewIds(customer.getCustomer_id(), questionIds,
                    PaySource.QUESTION);
            
            //查出所有问题
            List<AnswerView> ans = this.answerService.queryAnswersByQuestionIds(questionIds);
            List<Customer> cus = this.customerService.queryCustomersByIds(customerIds);
            for (Question q : questions) {
                QuestionView questionView = new QuestionView();
                //设置回答
                List<AnswerData> newAns = new ArrayList<>();
                for (AnswerView an : ans) {
                    if (an.getQuestion_id().equals(q.getQuestion_id())) {
                        newAns.add(an);
                        questionView.setQuestion_answer(newAns);
                        break;
                    }
                }
                ans.removeAll(newAns);
                if (questionView.getQuestion_answer() == null) {
                    questionView.setQuestion_answer(new ArrayList<AnswerData>());
                }
                //设置用户信息
                for (Customer c : cus) {
                    if (c.getCustomer_id().equals(q.getCustomer_id())) {
                        questionView.setCustomer_id(c.getCustomer_id());
                        questionView.setCustomer_avatar(c.getAvatar());
                        questionView.setCustomer_name(c.getCustomer_name());
                        break;
                    }
                }
                questionView.setUpvote_flag(ups.contains(q.getQuestion_id()) ? 1 : 0);
                questionView.setReward_flag(payViewIds.contains(q.getQuestion_id()) ? 1 : 0);
                questionView.setQuestion_id(q.getQuestion_id());
                questionView.setQuestion_content(q.getQuestion_content());
                questionView.setQuestion_date(DateTimeUtil.toString(q.getCreate_time(),
                        DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
                questionView.setPosition_str(q.getPosition_str());
                questionView.setAnswer_status(q.getAnswer_status());
                questionView.setPublic_status(q.getPublic_flag());
                questionView.setUpvote_num(q.getUpvote_num());
                questionView.setReward_num(q.getReward_num());
                questionView.setTotal_income(q.getTotal_income());
                dataList.add(questionView);
            }
            if (String.valueOf(QuestionRequestType.CUSTOMER.getVal()).equals(
                    String.valueOf(param.getRequest_type()))) {
                questionService.updateViewStatus(customer_id);
            }
        }
        
        result.newDataList().addAll(dataList);
        return VOUtil.setSuccessResult(result);
    }
    
    @Override
    public QuestionListResult funcQuestionAnswerListByDav(QuesAnsListForm param) {
        
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        String question_tag = param.getQuestion_tag();
        String position_str = param.getPosition_str();
        
        Integer request_num = SysContants.DEFAULT_REQUSET_NUM;
        if (!StringUtil.isEmpty(param.getRequest_num())) {
            request_num = Integer.parseInt(param.getRequest_num());
        }
        Question question = new Question();
        question.setBroker_id(broker_id);
        question.setPosition_str(position_str);
        question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
        if (!StringUtil.isEmpty(question_tag)) {
            question.setQuestion_tag(question_tag);
        }
        if (!StringUtil.isEmpty(param.getAnswer_status())
                && !String.valueOf(AnswerStatus.ALL.getVal()).equals(param.getAnswer_status())) {
            question.setAnswer_status(param.getAnswer_status());
        }
        Pagination page = new Pagination();
        page.setPage_size(request_num);
        question.setPage(page);
        
        List<QuestionView> dataList = new ArrayList<QuestionView>();
        
        List<Question> questions = this.questionService.query(question);
        Integer count = 0;
        //大V端需要查询数目
        count = questionService.countQuestion(question);
        //清空未读问题数目
        if (String.valueOf(AnswerStatus.NO_ANSWER.getVal()).equals(param.getAnswer_status())) {
            SessionHelper.getInstance().clearNoreadQuestionNum(broker_id);
        }
        if (questions != null && !questions.isEmpty()) {
            List<Long> questionIds = new ArrayList<>();
            List<Long> customerIds = new ArrayList<>();
            for (Question q : questions) {
                questionIds.add(q.getQuestion_id());
                if (!customerIds.contains(q.getCustomer_id())) {
                    customerIds.add(q.getCustomer_id());
                }
            }
            //查出所有问题
            List<AnswerView> ans = this.answerService.queryAnswersByQuestionIds(questionIds);
            List<Customer> cus = this.customerService.queryCustomersByIds(customerIds);
            for (Question q : questions) {
                QuestionView questionView = new QuestionView();
                //设置回答
                List<AnswerData> newAns = new ArrayList<>();
                for (AnswerView an : ans) {
                    if (an.getQuestion_id().equals(q.getQuestion_id())) {
                        newAns.add(an);
                        questionView.setQuestion_answer(newAns);
                        break;
                    }
                }
                ans.removeAll(newAns);
                if (questionView.getQuestion_answer() == null) {
                    questionView.setQuestion_answer(new ArrayList<AnswerData>());
                }
                //设置用户信息
                for (Customer c : cus) {
                    if (c.getCustomer_id().equals(q.getCustomer_id())) {
                        questionView.setCustomer_id(c.getCustomer_id());
                        questionView.setCustomer_avatar(c.getAvatar());
                        questionView.setCustomer_name(c.getCustomer_name());
                        break;
                    }
                }
                questionView.setQuestion_id(q.getQuestion_id());
                questionView.setQuestion_content(q.getQuestion_content());
                questionView.setQuestion_date(DateTimeUtil.toString(q.getCreate_time(),
                        DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
                questionView.setPosition_str(q.getPosition_str());
                questionView.setAnswer_status(q.getAnswer_status());
                questionView.setPublic_status(q.getPublic_flag());
                questionView.setUpvote_num(q.getUpvote_num());
                questionView.setReward_num(q.getReward_num());
                questionView.setTotal_income(q.getTotal_income());
                dataList.add(questionView);
            }
        }
        QuestionListResult result = new QuestionListResult();
        result.setCount(count);
        result.newDataList().addAll(dataList);
        return VOUtil.setSuccessResult(result);
    }
    
    @Override
    public HSBCPSBackVO funcAskQuestion(QuesAskForm param) {
        HSBCPSBackVO result = new HSBCPSBackVO();
        String question_content = param.getQuestion_content();
        
        if (StringUtils.isEmpty(question_content)) {
            return VOUtil.genErrorResultVo(HSBCPSErrorCodes.ClientErr.NOT_SUPPORT_FACE_OR_BLANK);
        }
        String public_flag = param.getPublic_flag();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        Customer customer = HSBCPSBizDataContext.getCustomer();
        
        Question question = new Question();
        question.setCustomer_id(customer.getCustomer_id());
        question.setBroker_id(broker_id);
        question.setPosition_str(PositionStrUtil.getPositionStr());
        question.setQuestion_content(question_content);
        question.setUpvote_num(0);
        question.setReward_num(0);
        question.setTotal_income(new BigDecimal(0));
        question.setCreate_time(new Date());
        question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
        question.setView_flag("0");
        question.setAnswer_status(String.valueOf(AnswerStatus.NO_ANSWER.getVal()));
        question.setPublic_flag(public_flag);
        
        questionService.save(question);
        
        CustomerBrokerRel rel = customerBrokerRelService.getByBrokerIdAndCustomerId(broker_id,
                customer.getCustomer_id());
        if (rel != null) {
            if (rel.getQuestion_num() == null) {
                rel.setQuestion_num(1);
            } else {
                rel.setQuestion_num(rel.getQuestion_num() + 1);
            }
            customerBrokerRelService.updateCAS(rel);
        }
        SessionHelper.getInstance().addBrokerNoreadQuestionNum(broker_id);
        //增加积分
        pointAddService.addPoint(customer.getMobile(), PointType.QUESTION, customer.getUser_id(),
                customer.getTenant_id());
        return VOUtil.setSuccessResult(result);
    }
    
    @Override
    public HSBCPSBackNumVO funcAnswerdNum(QuesAskNumForm param) {
        HSBCPSBackNumVO hsbcpsBackNumVO = new HSBCPSBackNumVO();
        Customer customer = HSBCPSBizDataContext.getCustomerNoException();
        int count = 0;
        
        if (null == customer) {
            hsbcpsBackNumVO.setNum(count);
        } else {
            Long user_id = customer.getCustomer_id();
            
            Question question = new Question();
            question.setAnswer_status(String.valueOf(AnswerStatus.ANSWERD.getVal()));
            question.setCustomer_id(user_id);
            question.setView_flag("0");
            question.setBroker_id(HSBCPSBizDataContext.getBrokerId());
            question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
            
            count = this.questionService.count(question);
        }
        hsbcpsBackNumVO.setNum(count);
        
        return VOUtil.setSuccessResult(hsbcpsBackNumVO);
    }
    
    @Override
    public HSBCPSBackVO funcQuestionAnswer(QuesAnswerForm param) {
        Long brokerId = HSBCPSBizDataContext.getBrokerId();
        Question q = questionService.get(param.getQuestion_id());
        if (q == null || DeleteStatus.DELETED.getVal() == ValueUtil.getInt(q.getDelete_flag())
                || !brokerId.equals(q.getBroker_id())) {
            return VOUtil.genErrorResultVo(QuestionErr.QUESTION_NOT_EXIST);
        }
        // 判断回答是否已经存在
        Answer example = new Answer();
        example.setQuestion_id(q.getQuestion_id());
        List<Answer> tempList = answerService.query(example);
        if (tempList != null && !tempList.isEmpty()) {
            return VOUtil.genErrorResultVo(QuestionErr.QUESTION_HAS_ANSERED);
        }
        Answer answer = new Answer();
        answer.setQuestion_id(Long.valueOf(param.getQuestion_id()));
        answer.setAnswer_content(param.getAnswer_content());
        //String publicStatus = param.getPublic_status();
        //answer.setPublic_flag(publicStatus);
        answer.setAnswer_tag(param.getAnswer_tag());
        answer.setBroker_id(brokerId);
        Date date = new Date();
        answer.setCreate_time(date);
        answerService.save(answer);
        answerNoticeAndUpdate(q, param.getAnswer_tag());
        //加入到搜索引擎
        Customer customer = customerService.get(q.getCustomer_id().toString());
        String customer_name = customer.getCustomer_name();
        Broker broker = brokerService.get(String.valueOf(q.getBroker_id()));
        String broker_name = broker.getBroker_name();
        String broker_avatar = broker.getBroker_avatar();
        SearchUtil.getInstance().addToSearch(q, answer, customer_name, broker_name, broker_avatar);
        return VOUtil.genSuccessBackVo();
    }
    
    private void answerNoticeAndUpdate(Question question, String tag) {
        // 如果回答是隐藏的 问题也设置为隐藏
        question.setAnswer_status(String.valueOf(AnswerStatus.ANSWERD.getVal()));
        question.setQuestion_tag(tag);
        question.setView_flag("0");
        question.setPosition_str(PositionStrUtil.getPositionStr());
        questionService.update(question);
    }
    
    @Override
    public HSBCPSBackVO funcQuestionDel(QuestionIdForm param) {
        HSBCPSBackVO hsbcpsBackVO = new HSBCPSBackVO();
        String id = param.getQuestion_id().toString();
        Question q = questionService.get(id);
        if (q == null || !HSBCPSBizDataContext.getBrokerId().equals(q.getBroker_id())
                || String.valueOf(DeleteStatus.DELETED.getVal()).equals(q.getDelete_flag())) {
            return VOUtil.genErrorResultVo(QuestionErr.QUESTION_NOT_EXIST);
        }
        questionService.delete(id);
        SearchUtil.getInstance().deleteToSearch(QUESTION_TYPE, id);
        return VOUtil.setSuccessResult(hsbcpsBackVO);
    }
    
    @Override
    public HSBCPSBackVO funcQuestionAnswerUpdate(QuesAnswerUpdateForm param) {
        HSBCPSBackVO hsbcpsBackVO = new HSBCPSBackVO();
        Answer answer = answerService.get(param.getAnswer_id());
        if (answer == null) {
            return VOUtil.genErrorResultVo(QuestionErr.ANSWER_NOT_EXIST);
        }
        Question q = questionService.get(String.valueOf(answer.getQuestion_id()));
        if (q == null || !HSBCPSBizDataContext.getBrokerId().equals(q.getBroker_id())) {
            return VOUtil.genErrorResultVo(QuestionErr.QUESTION_NOT_EXIST);
        }
        Date date = new Date();
        answer.setCreate_time(date);
        answer.setUpdate_time(new Date());
        answer.setAnswer_content(param.getAnswer_content());
        answer.setAnswer_tag(param.getAnswer_tag());
        //answer.setPublic_flag(param.getPublic_status().toString());
        answerService.update(answer);
        //更新搜索引擎
        SearchUtil.getInstance().updateToSearch(q, param);
        answerNoticeAndUpdate(q, answer.getAnswer_tag());
        return VOUtil.setSuccessResult(hsbcpsBackVO);
    }
    
    @Override
    public HSBCPSBackNumVO funcQuestionNoreadNum(HSBCPSVO param) {
        Integer num = SessionHelper.getInstance().getBrokerNoreadQuestionNum(HSBCPSBizDataContext.getBrokerId());
        HSBCPSBackNumVO hsbcpsBackVO = new HSBCPSBackNumVO();
        hsbcpsBackVO.setNum(num);
        return VOUtil.setSuccessResult(hsbcpsBackVO);
    }
    
    @Override
    public HSBCPSBackVO funcFansQuestionDel(QuestionIdForm param) {
        String question_id = param.getQuestion_id();
        Question question = questionService.get(question_id);
        Long customer_id = HSBCPSBizDataContext.getCustomer().getCustomer_id();
        if (question == null || !customer_id.equals(question.getCustomer_id())
                || String.valueOf(DeleteStatus.DELETED.getVal()).equals(question.getDelete_flag())) {
            return VOUtil.genErrorResultVo(QuestionErr.QUESTION_NOT_EXIST);
        }
        questionService.delete(question_id);
        SearchUtil.getInstance().deleteToSearch(QUESTION_TYPE, question_id);
        return VOUtil.genSuccessBackVo();
    }
    
    @Override
    public TenantQuestionListResult funcQuestionAnswerListByTenant(TenantKeyListQryForm param) {
        TenantQuestionListResult result = new TenantQuestionListResult();
        String position_str = param.getPosition_str();
        Integer request_num = Integer.parseInt(param.getRequest_num());
        List<TenantAnswerView> dataList = this.questionService.getTenantQuestionAnswerList(request_num,
                position_str);
        List<Long> questionIds = new ArrayList<>();
        for (TenantAnswerView tenantAnswerView: dataList) {
            questionIds.add(tenantAnswerView.getQuestion_id());
        }
        questionIds = ValueUtil.isEmpty(questionIds) ? null : questionIds;
        //查出点赞
        Customer customer = HSBCPSBizDataContext.getCustomerNoException();
        if (customer != null) {
            List<UpvoteWater> ups = this.upvoteWaterService.queryByCustomer(customer.getCustomer_id(),
                    questionIds, String.valueOf(UpvoteStatus.QUESTION_UPVOTE.getVal()));
            for (TenantAnswerView tenantAnswerView: dataList) {
                for (UpvoteWater up: ups) {
                    if (up.getTarget_id().equals(tenantAnswerView.getQuestion_id()) && up.getBroker_id().equals(tenantAnswerView.getBroker_id())) {
                        tenantAnswerView.setUpvote_flag("1");
                        break;
                    }
                }
                if (tenantAnswerView.getUpvote_flag() == null) {
                    tenantAnswerView.setUpvote_flag("0");
                }
            }
        }
        result.newDataList().addAll(dataList);
        return VOUtil.setSuccessResult(result);
    }
    
    @Override
    public HSBCPSBackVO funcAskQuestionByTenant(TenantQuesAskForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null != broker_id) {
            return VOUtil.genErrorResult(QuestionErr.STATUS_ERR, TenantQuestionListResult.class);
        }
        HSBCPSBackVO result = new HSBCPSBackVO();
        String question_content = param.getQuestion_content();
        
        String public_flag = param.getPublic_flag();
        broker_id = Long.valueOf(param.getBroker_id());
        Integer tenant_id = HSBCPSBizDataContext.getTenantId();
        Broker broker = brokerService.getById(broker_id, tenant_id);
        if (null == broker) {
            return VOUtil.genErrorResultVo(ClientErr.BROKER_NOT_FIND);
        }
        Long customer_id = HSBCPSBizDataContext.getCustomer().getCustomer_id();
        
        Question question = new Question();
        question.setCustomer_id(customer_id);
        question.setBroker_id(broker_id);
        question.setPosition_str(PositionStrUtil.getPositionStr());
        question.setQuestion_content(question_content);
        question.setUpvote_num(0);
        question.setReward_num(0);
        question.setTotal_income(new BigDecimal(0));
        question.setCreate_time(new Date());
        question.setDelete_flag(String.valueOf(DeleteStatus.NOT_DELETE.getVal()));
        question.setView_flag("0");
        question.setAnswer_status(String.valueOf(AnswerStatus.NO_ANSWER.getVal()));
        question.setPublic_flag(public_flag);
        
        questionService.save(question);
        
        CustomerBrokerRel rel = customerBrokerRelService.getByBrokerIdAndCustomerId(broker_id, customer_id);
        if (rel != null) {
            if (rel.getQuestion_num() == null) {
                rel.setQuestion_num(1);
            } else {
                rel.setQuestion_num(rel.getQuestion_num() + 1);
            }
            customerBrokerRelService.updateCAS(rel);
        }
        SessionHelper.getInstance().addBrokerNoreadQuestionNum(broker_id);
        Customer customer = HSBCPSBizDataContext.getCustomer();
        pointAddService.addPoint(customer.getMobile(), PointType.QUESTION, customer.getUser_id(),
                customer.getTenant_id());
        return VOUtil.setSuccessResult(result);
    }
    
}
